package com.yntx.usercenter.base.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yntx.usercenter.base.converter.SystemConverter;
import com.yntx.usercenter.base.dto.SystemDTO;
import com.yntx.usercenter.base.dto.SystemDeleteDTO;
import com.yntx.usercenter.base.dto.SystemUpdateDTO;
import com.yntx.usercenter.base.mapper.SystemMapper;
import com.yntx.usercenter.base.po.System;
import com.yntx.usercenter.base.service.SystemService;
import com.yntx.usercenter.common.config.TenantUserContextHolder;
import com.yntx.usercenter.common.util.KeyGenerateUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.Objects;

@Slf4j
@Service
@AllArgsConstructor
@DS("base")
public class SystemServiceImpl implements SystemService {

    private SystemMapper systemMapper;

    private final SystemConverter CONVERT = SystemConverter.INSTANCE;

    @Override
    public Long addSystem(SystemDTO dto) {
        checkParam(dto);
        checkData(dto);
        System system = CONVERT.systemDTO2System(dto);
        try {
            system.setAccessKey(KeyGenerateUtil.generateKey());
            system.setSecretKey(KeyGenerateUtil.generateKey());
            log.info("#addSystem# system:{}", system);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
        systemMapper.insert(system);
        return system.getId();
    }

    /**
     * 检查数据
     *
     * @param dto dto
     */
    @Override
    public void checkData(SystemDTO dto) {
        String systemCode = dto.getSystemCode();
        LambdaQueryWrapper<System> query = new LambdaQueryWrapper<System>()
                .eq(System::getTenantId, StringUtils.defaultString(TenantUserContextHolder.get().getTenantId(), dto.getTenantId()))
                .eq(System::getSystemCode, systemCode);
        System system = systemMapper.selectOne(query);
        if (Objects.nonNull(system)) {
            throw new RuntimeException("系统已存在！");
        }


    }

    /**
     * 检查参数
     *
     * @param dto dto
     */
    @Override
    public void checkParam(SystemDTO dto) {
        String tenantId = TenantUserContextHolder.get().getTenantId();
        if (StringUtils.isEmpty(tenantId) && StringUtils.isEmpty(dto.getSystemName())) {
            throw new RuntimeException("租户ID为空！");
        }
    }

    @Override
    public void deleteSystem(SystemDeleteDTO dto) {
        LambdaQueryWrapper<System> query = new LambdaQueryWrapper<System>()
                .eq(StringUtils.isNotEmpty(dto.getId()), System::getId, dto.getId())
                .eq(StringUtils.isNotEmpty(dto.getSystemCode()), System::getSystemCode, dto.getSystemCode())
                .eq(System::getTenantId, StringUtils.defaultString(TenantUserContextHolder.get().getTenantId(), dto.getTenantId()));
        systemMapper.deleteById(query);
    }

    @Override
    public void updateSystem(SystemUpdateDTO dto) {
        System system = CONVERT.systemUpdateDTO2System(dto);
        systemMapper.updateById(system);
    }

    @Override
    public List<System> selectSystem(SystemDTO dto) {
        LambdaQueryWrapper<System> query = new LambdaQueryWrapper<System>()
                .eq(StringUtils.isNotEmpty(dto.getSystemName()), System::getSystemName, dto.getSystemName())
                .eq(StringUtils.isNotEmpty(dto.getSystemCode()), System::getSystemCode, dto.getSystemCode())
                .eq(System::getTenantId, StringUtils.defaultString(TenantUserContextHolder.get().getTenantId(), dto.getTenantId()));
        return systemMapper.selectList(query);
    }

    @Override
    public System getById(Long id) {
        return systemMapper.selectById(id);
    }

}
